const { SERVER_ERR, USER_IS_NOT_EXIST, PASSWORD_IS_NOT_RIGHT } = require("../config/error.js");
const userService = require("../service/user.service.js");

class UserController {
  async create(ctx, next) {
    try {
      const { name = "", password = "", telPhone = "", avatar = "" } = ctx.request.body;
      const res = await userService.create({ name, password, telPhone, avatar });
      ctx.body = {
        code: 200,
        message: "创建成功",
        data: res,
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
  async delete(ctx, next) {
    try {
      const ids = ctx.request.body?.ids || [];
      const res = await userService.delete(ids);
      ctx.body = {
        code: 200,
        message: "删除成功",
        data: res,
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
  async page(ctx, next) {
    try {
      const { pageNum, pageSize, name, phone, email } = ctx.request.body;
      const res = await userService.page({ pageNum, pageSize, name, phone, email });
      const res2 = await userService.allNum();
      ctx.body = {
        code: 200,
        message: "查询成功",
        data: res,
        total: res2,
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
  async update(ctx, next) {
    try {
      const { id, name = "", telPhone = "", avatar = "" } = ctx.request.body;
      const res = await userService.update({
        id,
        name,
        telPhone,
        avatar,
      });
      ctx.body = {
        code: 200,
        message: "更新成功",
        data: res,
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
  async findName(ctx, next) {
    try {
      const { name } = ctx.request.body;
      const res = await userService.findName(name);
      ctx.body = {
        code: 200,
        message: "查询成功",
        data: res,
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
  async resetpassword(ctx, next) {
    try {
      const { id, oldPassword, newPassword } = ctx.request.body;
      const res1 = await userService.detail(id);
      if (!res1) {
        ctx.app.emit("error", USER_IS_NOT_EXIST, ctx);
        return;
      } else {
        const { password } = res1;
        if (oldPassword !== password) {
          ctx.app.emit("error", PASSWORD_IS_NOT_RIGHT, ctx);
          return;
        }
      }

      const res = await userService.resetpassword({
        id,
        password: newPassword,
      });
      ctx.body = {
        code: 200,
        message: "修改成功",
        data: res,
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
  async login(ctx, next) {
    const { token } = ctx.author;
    try {
      ctx.body = {
        code: 200,
        message: "登录成功",
        data: {
          token,
        },
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
  async userinfo(ctx, next) {
    try {
      ctx.body = {
        code: 200,
        message: "查询成功",
        data: ctx.user,
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
}

module.exports = new UserController();
